package com.oner365.test.function;

import java.util.function.Function;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.oner365.test.stream.ParallelStreamDemo;

//一个Function的demo
public class FunctionDemo {

  private static final Logger LOGGER = LoggerFactory.getLogger(ParallelStreamDemo.class);

  public static void main(String[] args) {
    String str = "test,123";
    //获取字符串中的数字
    int number = getNumber(str, s -> Integer.parseInt(str.split(",")[1]));
    LOGGER.info("number:{}", number);
    //获取字符串中的数字、转型成int、将数字增加10
    LOGGER.info("number:{}", getNumber(str, s -> str.split(",")[1], s -> Integer.parseInt(s), s -> s += 10));
  }

  // 执行获取字符串中数字的function
  public static int getNumber(String str, Function<String, Integer> fun) {
    return fun.apply(str);
  }

  // 执行获取字符串中数字的function
  // Function<String,String>
  // 入参String类型，result返回一个String类型
  public static int getNumber(String str, Function<String, String> splitFun, Function<String, Integer> parseFun,
      Function<Integer, Integer> plusFun) {
    return splitFun.andThen(parseFun).andThen(plusFun).apply(str);
  }
}
